package de.deliancourt.jarvis.implementations;

import javax.media.j3d.Appearance;
import javax.media.j3d.Geometry;
import javax.media.j3d.PointArray;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.RenderingAttributes;
import javax.media.j3d.Shape3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TriangleArray;
import javax.vecmath.Point2d;
import javax.vecmath.Point2f;
import javax.vecmath.Point3d;
import javax.vecmath.Point3f;
import javax.vecmath.TexCoord2f;

import com.sun.j3d.utils.geometry.Primitive;

public class Tile extends Primitive {

	public static final Integer YZ=0;
	public static final Integer XY=1;
	public static final Integer XZ=2;

	private Shape3D shape = new Shape3D();
	private TriangleArray Box = new TriangleArray(6, TriangleArray.COORDINATES | TriangleArray.TEXTURE_COORDINATE_2 );
	private TransformGroup tGroup = new TransformGroup();


	public void GenerateTile(Point3d TopLeft, double A1, double A2, Integer Axis) {

		//System.out.print("Adding Tile at\nx:" + TopLeft.x + "\ny: " +TopLeft.y +"\nz: " +TopLeft.z + "\nA1: " + A1 + "\nA2: " + A2 + "\nOn Axis: " +Axis+ "\n");

		if (Axis==0){
			Box.setCoordinate(0, new Point3d(TopLeft.x,TopLeft.y,TopLeft.z)); //Oben links
			Box.setCoordinate(1, new Point3d(TopLeft.x,TopLeft.y,TopLeft.z-A2)); // unten links
			Box.setCoordinate(2, new Point3d(TopLeft.x,TopLeft.y-A1,TopLeft.z-A2)); //oben rechts
			Box.setCoordinate(3, new Point3d(TopLeft.x,TopLeft.y-A1,TopLeft.z-A2)); //Oben links
			Box.setCoordinate(4, new Point3d(TopLeft.x,TopLeft.y-A1,TopLeft.z)); // unten links
			Box.setCoordinate(5, new Point3d(TopLeft.x,TopLeft.y,TopLeft.z)); //oben rechts
		}
		else
		{
			double Width=0;
			double Height=0;
			double Depth=0;

			if(Axis==2) {
				Width=A1;
				Depth=A2;
			}
			if(Axis==1) {
				Width=A1;
				Height=A2;
			}
			Box.setCoordinate(0, new Point3d(TopLeft.x,TopLeft.y,TopLeft.z)); //Oben links
			Box.setCoordinate(1, new Point3d(TopLeft.x,TopLeft.y-Height,TopLeft.z-Depth)); // unten links
			Box.setCoordinate(2, new Point3d(TopLeft.x+Width,TopLeft.y-Height,TopLeft.z-Depth)); //oben rechts
			Box.setCoordinate(3, new Point3d(TopLeft.x+Width,TopLeft.y-Height,TopLeft.z-Depth)); //Oben links
			Box.setCoordinate(4, new Point3d(TopLeft.x+Width,TopLeft.y,TopLeft.z)); // unten links
			Box.setCoordinate(5, new Point3d(TopLeft.x,TopLeft.y,TopLeft.z)); //oben rechts
		}

		TexCoord2f[] texCoords={
				new TexCoord2f(0.0f,0.0f),
				new TexCoord2f(0.0f,1.0f),
				new TexCoord2f(1.0f,1.0f),
				new TexCoord2f(1.0f,1.0f),
				new TexCoord2f(1.0f,0.0f),
				new TexCoord2f(0.0f,0.0f),
		};
		Box.setTextureCoordinates(0,0, texCoords);		

		shape.setGeometry(Box);
		//tGroup.addChild(shape);
		//this.addChild(shape);

	}

	public TransformGroup gettGroup() {
		return tGroup;
	}

	public Tile(double x, double y, double z, double A1, double A2, Integer Axis) {
		GenerateTile(new Point3d(x,y,z),A1,A2,Axis);
	}
	public Tile (Point3d TopLeft, Point2d WHD, Integer Axis){
		GenerateTile(TopLeft, WHD.x, WHD.y, Axis);
	}

	public Geometry getGeometry(){
		return shape.getGeometry();
	}
	public Point2d GetlocalMidPoint(){

		Point3d first= new Point3d();
		Point3d last= new Point3d();

		Box.getCoordinate(0, first);
		Box.getCoordinate(3, last);

		return new Point2d (last.x-first.x,last.y-first.y) ;
	}
	public Point3d GetCoords(Integer Pos){
		Point3d tmp = new Point3d();
		Box.getCoordinate(Pos, tmp);
		return  tmp;
	}
	
	public void SetCoords(Integer Pos, Point3d c ){
		Box.setCoordinate(Pos, c);
	}

	public Point2d GetWorldMidPoint(){

		Point3d first= new Point3d();
		Point3d last= new Point3d();

		Box.getCoordinate(0, first);
		Box.getCoordinate(3, last);

		return new Point2d ((last.x-first.x)+first.x,(last.y-first.y)+first.y) ;
	}


	public void setTexCoordinates(Integer Index, Integer Start, TexCoord2f[] Coords ){
		Box.setTextureCoordinates(Index, Start, Coords);

	}

	@Override
	public Appearance getAppearance(int Pos) {
		if (Pos!=0) return null;
		return shape.getAppearance();
	}

	@Override
	public Shape3D getShape(int Pos) {
		if (Pos!=0) return null;
		return shape;
	}

	@Override
	public void setAppearance(Appearance App) {
		shape.setAppearance(App);
	}

}
